<?php
require_once 'PerformanceData.php';

class Admin_ChartController extends Zend_Controller_Action
{
    public $contexts = array('browse' => array('xml'));

    function init()
    {
        $this->getHelper('layout')->setLayout('json');
    }
        
    public function performanceAction()
    {
        $id = $this->_request->getParam('id');
        $subject = $this->_request->getParam('Subject');
        $metric = $this->_request->getParam('Metric');
        $mode = $this->_request->getParam('Mode');
        
        $legend = ($this->_request->getParam('Legend') == 1) ? true : false;
        //$colorOffset = $this->_request->getParam('ColorOffset') ? $this->_request->getParam('ColorOffset') : 0;
        
        switch ($mode) {
            case 'Day': $title = $this->view->translate('Last 7 days'); break;
            case 'Week': $title = $this->view->translate('Last 4 weeks'); break;
            case 'Month': $title = $this->view->translate('Last 12 months'); break;
        }
        
        $model = new PerformanceData($subject);
        
        $data = $model->chartData($id,$metric ? array($metric) : array(), array('type' => 'barStack', 'mode' => $mode));
        $meta = $model->chartMeta(); 
        
        foreach(array_keys($data) as $k) unset($data[$k]['Score']);
        
        $this->_helper->chart->barStack($data,$mode,$this->view->translate('Score'),array(
            'legend' => $legend, 'alpha' => 0.7, 'color_offset' => 4)); 
        
        if($subject == 'Officer') {
            $users = new Zend_Db_Table(array('name' => 'users', 'primary' => 'UserId'));
            $user = $users->find($id)->current();
            $model2 = new PerformanceData('Location');
            $ldata = $model2->chartData($user->LocationId,array('Score'), array('type' => 'areaStack', 'mode' => $mode));
            $this->_helper->chart->line($ldata,$mode,$this->view->translate('Score'),array('legend' => $legend)); 
        }
        if($subject == 'Program') {
            $programs = new Zend_Db_Table(array('name' => 'programs', 'primary' => 'ProgramId'));
            $program = $programs->find($id)->current();
            $model2 = new PerformanceData('Line');
            $ldata = $model2->chartData($program->ProductLineId,array('Score'), array('type' => 'areaStack', 'mode' => $mode));
            $this->_helper->chart->line($ldata,$mode,$this->view->translate('Score'),array('legend' => $legend)); 
        }
        
        $chart = $this->_helper->chart->setYAxisRange(0,$meta['Score']['max'],$meta['Score']['step']);
        $title = new OFC_Elements_Title($title);
        $title->set_style( "{font-size: 10px; font-weight: bold; color: #444444;}" );
        $chart->set_title($title);
        echo $chart->toPrettyString(); 
        exit;                
    } 
    
        
}
